\documentclass[11pt]{ctexart}
\usepackage{tabularx}
\usepackage{array}
\usepackage{bm}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{graphicx}
\usepackage[table,xcdraw]{xcolor}
%\usepackage{algorithm2e}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{fancyhdr}
\pagestyle{fancy}

\hypersetup{hypertex=true,
	colorlinks=true,
	linkcolor=red,
	anchorcolor=blue,
	citecolor=blue}

\fancyhf{}
\chead{\textbf{计算方法}}
\fancyhead[r]{\bfseries\thepage}
\fancyhead[l]{\bfseries\rightmark}
\renewcommand{\headrulewidth}{0.4pt} % 注意不用 \setlength
\renewcommand{\footrulewidth}{0pt}

\floatname{algorithm}{算法}
\renewcommand{\algorithmicrequire}{\textbf{输入:}}
\renewcommand{\algorithmicensure}{\textbf{输出:}}
\begin{titlepage}
	\title{\Huge\textbf{计算方法四、五章作业}\\}
	\author{\Large\textbf{作者}：吴润泽 \and{\Large\textbf{学号}：181860109}\\
		\\
		\and {\Large\textbf{Email}：\href{mailto:181860109@smail.nju.edu.cn}{181860109@smail.nju.edu.cn}}\\}
	\date{\Large\today}
\end{titlepage}
\begin{document}
		\maketitle
	\tableofcontents
	\newpage
	\section*{assignment I}
	\markright{assignment I}
	\addcontentsline{toc}{section}{assignment I}
	\subsection*{problem 1}
	\markright{problem 1}
    \addcontentsline{toc}{subsection}{problem 1}
    \paragraph*{解：}将$f(x)=1$代入公式，得
    $$2=\int_{-1}^1\mathrm{d}x=[f(-1)+2(f(x_1)+3f(x_2)]/3=2$$，
    则令公式在$f(x)=x,x^2$时均成立，得
    $$
	\left\{
	\begin{aligned}
    &-1+2x_1+3x_2=0,\\
    &1+2x_1^2+3x_2^2=2,
	\end{aligned}
	\right.
    $$
    解得$x_1=-0.2898979,x_2=0.5265986$或$x_1=0.6898979,x_2=-0.1265986$。\\
    将$f(x)=x^2$代入公式，得
    $$\int_1^1x^3\mathrm{d}x=\frac{1}{2}\neq\frac{1}{3}=[f(-1)+2(f(x_1)+3f(x_2)]/3.$$
    故求积公式具有2次代数精度，求积公式为
    $$\int_{-1}^1f(x)\mathrm{d}x\approx \frac{1}{3}[f(-1)+2f(-0.2898979)+3f(0.5265986)],$$
    或者
    $$\int_{-1}^1f(x)\mathrm{d}x\approx \frac{1}{3}[f(-1)+2f(0.6898979)+3f(-0.1265986)].$$
    \subsection*{problem 2}
	\markright{problem 2}
    \addcontentsline{toc}{subsection}{problem 2}
    \paragraph*{(1)}用复合梯形公式，$h=\frac{1}{8},f(x)=\frac{x}{4+x^2},x_k=\frac{k}{8}(k=1,2,\cdots,7)$，
    $$T_8=\frac{h}{2}[f(0)+2\sum\limits_{k=1}^7f(x_k)+f(1)]=0.1114024.$$
    用复合辛普森公式，$h=\frac{1}{8},x_{k+\frac{1}{2}}=\frac{2k+1}{16}(k=0,1,\cdots,7)$，
    $$S_8=\frac{h}{6}[f(0)+4\sum\limits_{k=0}^7f(x_{k+\frac{1}{2}})+2\sum\limits_{k=1}^7f(x_k)+f(1)]=0.1115718.$$
    \subsection*{problem 7}
	\markright{problem 7}
    \addcontentsline{toc}{subsection}{problem 7}
    \paragraph*{解}因为$f(x)=e^x$，则$f^{''}(x)=f^{(4)}(x)=e^x$，设将$[0,1]$区间划分为n等份，则$h=\frac{1}{n}$，
    对于复合梯形公式，则有
    $$\left|R_T(f)\right|=\left|-\frac{b-a}{12}h^2f^{''}(\eta )\right|
    \leq\frac{1}{12}(\frac{1}{n})^2 e\leq\frac{1}{2}\times 10^{-5},
    \eta\in(0,1),$$
    解得$n\geq 212.85$，则取$n=213$，即将$[0,1]$区间划分为213等份。\\
    对于复合辛普森公式，则有
    $$\left|R_S(f)\right|=\left|-\frac{b-a}{180}(\frac{h}{2})^2f^{(4)}(\eta )\right|
    \leq\frac{1}{2880}(\frac{1}{n})^4 e\leq\frac{1}{2}\times 10^{-5},
    \eta\in(0,1),$$
    解得$n\geq 3.7066$，则取$n=4$，即实际上将$[0,1]$区间划分为8等份即可。\\
    \subsection*{problem 11}
	\markright{problem 11}
    \addcontentsline{toc}{subsection}{problem 11}
    \subsubsection*{(1)}
    \begin{tabular}{|l|l|l|l|l|l|}
        \hline
        {\color[HTML]{000000} \textbf{k}} & {\color[HTML]{000000} \textbf{$T_0^{(k)}$}} & {\color[HTML]{000000} \textbf{$T_1^{(k)}$}} & {\color[HTML]{000000} \textbf{$T_2^{(k)}$}} & {\color[HTML]{000000} \textbf{$T_3^{(k)}$}} & {\color[HTML]{000000} \textbf{$T_4^{(k)}$}} \\ \hline
        {\color[HTML]{000000} 0} & {\color[HTML]{000000} 1.3333333} & {\color[HTML]{000000} } & {\color[HTML]{000000} } & {\color[HTML]{000000} } & {\color[HTML]{000000} } \\
        {\color[HTML]{000000} 1} & {\color[HTML]{000000} 1.1666667} & {\color[HTML]{000000} 1.1111111} & {\color[HTML]{000000} } & {\color[HTML]{000000} } & {\color[HTML]{000000} } \\ 
        {\color[HTML]{000000} 2} & {\color[HTML]{000000} 1.1166667} & {\color[HTML]{000000} 1.1000000} & {\color[HTML]{000000} 1.0992593} & {\color[HTML]{000000} } & {\color[HTML]{000000} } \\
        {\color[HTML]{000000} 3} & {\color[HTML]{000000} 1.1032107} & {\color[HTML]{000000} 1.0987253} & {\color[HTML]{000000} 1.0986403} & {\color[HTML]{000000} 1.0986305} & {\color[HTML]{000000} } \\ 
        {\color[HTML]{000000} 4} & {\color[HTML]{000000} 1.0997677} & {\color[HTML]{000000} 1.0986200} & {\color[HTML]{000000} 1.0986130} & {\color[HTML]{000000} 1.0986126} & {\color[HTML]{000000} 1.0986125} \\ \hline
        \end{tabular}\\
    \hspace*{20pt}因此可以选择$I=1.0986125$。
    \subsubsection*{(2)}
    先将区间$[0,\frac{\pi}{2}]$化为$[-1,1]$，有$\int_1^3\frac{\mathrm{d}y}{y}
    =\int_{-1}^1\frac{\mathrm{d}t}{t+2}.$\\
    三点高斯公式
$$\begin{aligned}
    \int_{-1}^1\frac{\mathrm{d}t}{t+2}\approx&0.5555556\times(\frac{1}{2-0.7745967}+\frac{1}{2+0.7745967})\\
    &+0.8888889\times\frac{1}{2+0}=1.0980393.
\end{aligned}$$
    五点高斯公式
    $$\begin{aligned}
        \int_{-1}^1\frac{\mathrm{d}t}{t+2}\approx&
        0.2369269\times(\frac{1}{2-0.9061798}+\frac{1}{2+0.9061798})\\
        &+0.4786289\times(\frac{1}{2-0.5384693}+\frac{1}{2+0.5384693})\\
        &+0.5688889\times\frac{1}{2+0}=1.0986093.
    \end{aligned}$$
    \subsubsection*{(3)}
    将区间$[1,3]$划分为四等份，每份上使用两点高斯公式
    $$\begin{aligned}
        I_1&=\int_1^{1.5}\frac{\mathrm{d}y}{y}=
        \int_{-1}^{1}\frac{0.5\mathrm{d}t}{0.5t+2.5}\\
        &\approx 0.5\times\left\{\frac{1}{2.5-0.5\times\frac{1}{\sqrt{3}}}
        +\frac{1}{2.5+0.5\times\frac{1}{\sqrt{3}}}\right\}=0.4054054.\\
    \end{aligned}$$
    $$\begin{aligned}
        I_2&=\int_{1.5}^{2}\frac{\mathrm{d}y}{y}=
        \int_{-1}^{1}\frac{0.5\mathrm{d}t}{0.5t+3.5}\\
        &\approx 0.5\times\left\{\frac{1}{3.5-0.5\times\frac{1}{\sqrt{3}}}
        +\frac{1}{3.5+0.5\times\frac{1}{\sqrt{3}}}\right\}=0.2876712.\\
    \end{aligned}$$
    $$\begin{aligned}
        I_3&=\int_{2}^{2.5}\frac{\mathrm{d}y}{y}=
        \int_{-1}^{1}\frac{0.5\mathrm{d}t}{0.5t+4.5}\\
        &\approx 0.5\times\left\{\frac{1}{4.5-0.5\times\frac{1}{\sqrt{3}}}
        +\frac{1}{4.5+0.5\times\frac{1}{\sqrt{3}}}\right\}=0.2231405.\\
    \end{aligned}$$
    $$\begin{aligned}
        I_4&=\int_{2.5}^{3}\frac{\mathrm{d}y}{y}=
        \int_{-1}^{1}\frac{0.5\mathrm{d}t}{0.5t+5.5}\\
        &\approx 0.5\times\left\{\frac{1}{5.5-0.5\times\frac{1}{\sqrt{3}}}
        +\frac{1}{5.5+0.5\times\frac{1}{\sqrt{3}}}\right\}=0.1823204.\\
    \end{aligned}$$
    因此$I=I_1+I_2+I_3+I_4\approx1.0985376.$
    \subsection*{problem 12}
	\markright{problem 12}
    \addcontentsline{toc}{subsection}{problem 12}
    \paragraph*{三点求导公式}
    $$\begin{aligned}
    &f^{'}(x_0)\approx\frac{1}{2h}[-3f(x_0)+4f(x_1)-f(x_2)]\\
    &f^{'}(x_1)\approx\frac{1}{2h}[-f(x_0)+f(x_2)]\\
    &f^{'}(x_2)\approx\frac{1}{2h}[f(x_0)-4f(x_1)+3f(x_2)]\\
    \end{aligned}$$
    解得
    $$f^{'}(x_0)\approx-0.247,f^{'}(x_1)\approx-0.217,f^{'}(x_2)\approx-0.187.$$
    \paragraph*{五点求导公式}
    $$\begin{aligned}
    P_4^{'}(x_0+th)=
    \frac{1}{12h}[&(2t^3-15t^2+35t-25)f(x_0)+(-8t^3+54t^2-104t+48)f(x_1)\\
    &+12(t^3-6t^2+\frac{19}{2}t-3)f(x_2)+2(-4t^3+21t^2-28t+8)f(x_3)\\
    &+(2t^3-9t^2+11t-3)f(x_4)]
    \end{aligned}$$
    $$\begin{aligned}
        &f^{'}(x_0)\approx\frac{1}{12h}[-25f(x_0)+48f(x_1)-36f(x_2)+16f(x_3)-3f(x_4)]\\
        &f^{'}(x_1)\approx\frac{1}{12h}[-3f(x_0)-10f(x_1)+18f(x_2)-6f(x_3)+f(x_4)]\\
        &f^{'}(x_2)\approx\frac{1}{12h}[f(x_0)-8f(x_1)+8f(x_3)-f(x_4)]\\
    \end{aligned}$$
    解得
    $$f^{'}(x_0)\approx-0.2483,f^{'}(x_1)\approx-0.2163,f^{'}(x_2)\approx-0.1883.$$
    \paragraph*{$x=1.0$的误差}$$|f^{'''}(\xi)|
    \leq\max\limits_{1.0\leq x\leq1.2}|f^{'''}(x)|
    \leq\max\limits_{1.0\leq x\leq1.2}|\frac{4!}{(1+x)^5}|=0.75,$$
    对于$f^{'}(x_0)$余项为$\frac{h^2}{3}f^{'''}(\xi)$，因此误差上限为0.0025。
    \section*{assignment II}
	\markright{assignment II}
	\addcontentsline{toc}{section}{assignment II}
	\subsection*{problem 2}
	\markright{problem 2}
    \addcontentsline{toc}{subsection}{problem 2}
    \paragraph*{解}改进欧拉法公式
    $\left\{
        \begin{aligned}
            &\overline{y}_{n+1}=y_n+h(x_n+y_n)\\
            &y_{n+1}=y_n+\frac{h}{2}[x_n+y_n+x_{n+1}+\overline{y}_{n+1}]\\
        \end{aligned}
        \right.
    $得
    $$\overline{y}_{n+1}=\frac{2+2h+h^2}{2}y_n+\frac{h+h^2}{2}x_n+\frac{h}{2}x_{n+1}.$$
    解得
        \begin{tabular}{|c|c|c|c|}
        \hline
        {\color[HTML]{000000} \textbf{$x_n$}} & {\color[HTML]{000000} \textbf{改进欧拉法$y_n$}} & {\color[HTML]{000000} \textbf{$y(x_n)$}} & {\color[HTML]{000000} \textbf{$|y(x_n)-y_n|$}} \\ \hline
        {\color[HTML]{000000} 0.1} & {\color[HTML]{000000} 1.110000000} & {\color[HTML]{000000} 1.110341836} & {\color[HTML]{000000} $3.418361513\times10^{-4}$} \\ \hline
        {\color[HTML]{000000} 0.2} & {\color[HTML]{000000} 1.242050000} & {\color[HTML]{000000} 1.242805516} & {\color[HTML]{000000} $7.555163203\times10^{-4}$} \\ \hline
        {\color[HTML]{000000} 0.3} & {\color[HTML]{000000} 1.398465250} & {\color[HTML]{000000} 1.399717615} & {\color[HTML]{000000} $1.252365152\times10^{-3}$} \\ \hline
        {\color[HTML]{000000} 0.4} & {\color[HTML]{000000} 1.581804101} & {\color[HTML]{000000} 1.583649395} & {\color[HTML]{000000} $1.845294033\times10^{-3}$} \\ \hline
        {\color[HTML]{000000} 0.5} & {\color[HTML]{000000} 1.794893532} & {\color[HTML]{000000} 1.797442541} & {\color[HTML]{000000} $2.549009519\times10^{-3}$} \\ \hline
        \end{tabular}\\
    在$x=0.5$，改进欧拉法解得$y(0.5)=1.794893532$，\\
    绝对误差为$$\delta{y_n}=|y(x_n)-y_n|=2.549009519\times10^{-3},$$
    相对误差为$$\frac{\delta{y_n}}{y_n}=\frac{2.549009519\times10^{-3}}{1.797442541}=1.418131295\times10^{-3}.$$
    \subsection*{problem 6}
	\markright{problem 6}
    \addcontentsline{toc}{subsection}{problem 6}
    \paragraph*{解}
    取经典的四阶Runge-Kutto公式为
    $$\left\{
        \begin{aligned}
            &y_{n+1}=y_n+\frac{h}{6}(K_1+2K_2+2K_3+K_4),\\
            &K_1=f(x_n,y_n),\\
            &K_2=f(x_n+\frac{h}{2},y_n+\frac{h}{2}K_1),\\
            &K_3=f(x_n+\frac{h}{2},y_n+\frac{h}{2}K_2),\\
            &K_4=f(x_n+h,y_n+hK_3).
        \end{aligned}
        \right.
    $$
    解得$$
       \begin{aligned}
        &x=0.2,y=1.727548209,&x=0.4,y=2.742951299,\\
        &x=0.6,y=4.094181355,&x=0.8,y= 5.829210728,\\
        &x=1.0,y=7.996012143 \\
       \end{aligned} 
    $$
    \subsection*{problem 9}
	\markright{problem 9}
    \addcontentsline{toc}{subsection}{problem 9}
    \paragraph*{解}
    二阶显式Adams方法：$y_{n+2}=y_{n+1}+\frac{h}{2}(3f_{n+1}-f_{n}),$\\
    二阶隐式Adams方法：$y_{n+1}=y_{n}+\frac{h}{2}(f_{n+1}+f_n).$\\
    代入$f=1-y$得\\
    二阶显式Adams方法：$y_{n+2}=(1-\frac{3}{2}h)y_{n+1}+\frac{h}{2}y_n+h$\\
    二阶隐式Adams方法：$y_{n+1}=\frac{2-h}{2+h}y_{n}+\frac{2h}{2+h}.$\\
    取$h=0.2,y_0=0,y_1=0.181$，得到结果如下图
    \begin{table}[htp]
        \begin{tabular}{|c|c|c|c|c|c|}
            \hline
            {\color[HTML]{000000} \textbf{$x_n$}} & {\color[HTML]{000000} \textbf{精确解$y(x_n)$}} & {\color[HTML]{000000} \textbf{显式$y_n$}} & {\color[HTML]{000000} \textbf{$|y(x_n)-y_n|$}} & {\color[HTML]{000000} \textbf{隐式$y_n$}} & {\color[HTML]{000000} \textbf{$|y(x_n)-y_n|$}} \\ \hline
            {\color[HTML]{000000} 0.4} & {\color[HTML]{000000} 0.329679954} & {\color[HTML]{000000} 0.326700000} & {\color[HTML]{000000} $2.979953964\times10^{-3}$} & {\color[HTML]{000000} 0.329909091} & {\color[HTML]{000000} $2.291369447\times10^{-4}$} \\ \hline
            {\color[HTML]{000000} 0.6} & {\color[HTML]{000000} 0.451188364} & {\color[HTML]{000000} 0.446790000} & {\color[HTML]{000000} $4.398363906\times10^{-3}$} & {\color[HTML]{000000} 0.451743802} & {\color[HTML]{000000} $5.554377469\times10^{-4}$} \\ \hline
            {\color[HTML]{000000} 0.8} & {\color[HTML]{000000} 0.550671036} & {\color[HTML]{000000} 0.545423000} & {\color[HTML]{000000} $5.248035883\times10^{-3}$} & {\color[HTML]{000000} 0.551426747} & {\color[HTML]{000000} $7.557109241\times10^{-4}$} \\ \hline
            {\color[HTML]{000000} 1.0} & {\color[HTML]{000000} 0.632120559} & {\color[HTML]{000000} 0.626475100} & {\color[HTML]{000000} $5.645458829\times10^{-3}$} & {\color[HTML]{000000} 0.632985520} & {\color[HTML]{000000} $8.649612862\times10^{-4}$} \\ \hline
        \end{tabular}
    \end{table}
    \paragraph*{因此}显式Adams：$y(1.0)=0.62647510$，
    隐式Adams：$y(1.0)=0.632985520$。通过绝对误差比较可知隐式方法比显式方法更准确。
    \subsection*{problem 补充习题}
	\markright{problem 补充习题}
    \addcontentsline{toc}{subsection}{problem 补充习题}
    \paragraph*{解}
    二阶Taylor展开公式为
    $$y(x_{n+1})=y(x_n)+h y^{'}(x_n)+\frac{h^2}{2}y^{''}(x_n)+O(h^3),$$
    三阶Taylor展开公式为
    $$y(x_{n+1})=y(x_n)+h y^{'}(x_n)+\frac{h^2}{2}y^{''}(x_n)+
    \frac{h^3}{3!}y^{'''}(x_n)+O(h^4),$$
    $\left\{\begin{aligned}
        &y^{'}=f=f^(0)=e^{-x^2},\\
        &y^{''}=\frac{\partial f}{\partial x}+f\frac{\partial f}{\partial y}=-2xe^{-x^2}=f^{(1)},\\
        &y^{'''}=\frac{\partial f^{(1)}}{\partial x}+f\frac{\partial f^{(1)}}{\partial y}=(4x^2-2)e^{-x^2}.
    \end{aligned}\right.
    $
    代入Taylor展开公式可得，\\
    二阶方法：
    $$y_{n+1}=y_n+he^{-x_n^2}+-{h^2}x_ne^{-x_n^2},$$
    三阶方法：
    $$y_{n+1}=y_n+he^{-x_n^2}+-{h^2}x_ne^{-x_n^2}+\frac{h^3}{3}(2x_n^2-1)e^{-x_n^2}.$$
\end{document}